package jee.boot.encrypt.webInterceptor;

import jee.boot.common.basic.Page;
import jee.boot.common.basic.result.ResponseMessage;
import jee.boot.encrypt.config.DataEncryptConfig;
import jee.boot.encrypt.config.IEncryptDecrypt;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author
 * @desc 返回数据加密
 */
@ControllerAdvice(basePackages = "jee.boot")
public class WebResponseBodyAdvice implements ResponseBodyAdvice {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private DataEncryptConfig dataEncryptConfig;
    @Autowired
    private IEncryptDecrypt encryptDecrypt;

    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        if (dataEncryptConfig.getEnableEncryptedTransmission()) {
            if (Objects.nonNull(body)) {
                // 参数如果是数组,则循环进行解密
                if (body instanceof ArrayList) {
                    ArrayList resultList = (ArrayList) body;
                    if (CollectionUtils.isNotEmpty(resultList) && encryptDecrypt.needToDecrypt(resultList.get(0))) {
                        logger.debug("传输-拦截响应数据进行加密处理");
                        for (Object o : resultList) {
                            encryptDecrypt.encrypt(o);
                        }
                    }
                    // 如果返回的数据是ResponseMessage
                } else if (body instanceof ResponseMessage) {
                    Object data = ((ResponseMessage) body).getData();
                    // 如果返回的数据是Page
                    if (data instanceof Page) {
                        List list = ((Page) data).getList();
                        if (CollectionUtils.isNotEmpty(list) && encryptDecrypt.needToDecrypt(list.get(0))) {
                            logger.debug("传输-拦截响应数据进行加密处理");
                            for (Object o : list) {
                                encryptDecrypt.encrypt(o);
                            }
                        }
                    } else {
                        // 加密
                        if (encryptDecrypt.needToDecrypt(data)) {
                            logger.debug("传输-拦截响应数据进行加密处理");
                            encryptDecrypt.encrypt(data);
                        }
                    }

                } else {
                    // 解密
                    if (encryptDecrypt.needToDecrypt(body)) {
                        logger.debug("传输-拦截响应数据进行加密处理");
                        encryptDecrypt.encrypt(body);
                    }
                }

            }
        }

        return body;
    }
}